﻿//http://vilebody.wordpress.com/2007/11/07/flash-actionscript-3-proportional-image-resizing/

package{
	import flash.display.*;
	import flash.geom.Matrix;

	public class ResizeImage extends Sprite {
		public static function resizeIt(b:*, maxH:int,maxW:int):BitmapData {
			var thisWidth:int = Math.round(b.width);
			var thisHeight:int = Math.round(b.height);
			var r:Number;//ratio
			r = thisWidth / thisHeight;//calculation ratio to which resize takes place

			if (thisWidth > maxW) {
				thisWidth = maxW;
				thisWidth = Math.round(thisWidth * r);
			}
			if (thisHeight > maxH) {
				thisHeight = maxH;
				thisWidth = Math.round(thisHeight/r);
			}
			var bd:BitmapData = new BitmapData(thisWidth,thisHeight,true,0x00FFFFFF);// transparent, in case your image is…
			var matrix:Matrix = new Matrix();
			var sx:int = thisWidth / b.width;
			var sy:int = thisHeight / b.height;
			trace("x="+sx+" y="+sy);
			matrix.scale(sx, sy);
			bd.draw(b,matrix);
			return bd;
		}
	}
}